<template>
	<a-drawer
		:title="formData.commentId ? '编辑应用评论' : '增加应用评论'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="应用id：" name="appId">
				<a-input v-model:value="formData.appId" placeholder="请输入应用id" allow-clear/>
			</a-form-item>
			<a-form-item label="评论内容：" name="commentContent">
				<a-input v-model:value="formData.commentContent" placeholder="请输入评论内容" allow-clear/>
			</a-form-item>
			<a-form-item label="评论分数：" name="commentScore">
				<a-input v-model:value="formData.commentScore" placeholder="请输入评论分数" allow-clear/>
			</a-form-item>
			<a-form-item label="评论时间：" name="commentTime">
				<a-date-picker v-model:value="formData.commentTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择评论时间" style="width: 100%"/>
			</a-form-item>
			<a-form-item label="评论用户id：" name="commentUser">
				<a-input v-model:value="formData.commentUser" placeholder="请输入评论用户id" allow-clear/>
			</a-form-item>
			<a-form-item label="评论用户手机号：" name="commentUserPhone">
				<a-input v-model:value="formData.commentUserPhone" placeholder="请输入评论用户手机号" allow-clear/>
			</a-form-item>
			<a-form-item label="是否回复 0:未回复 1:已回复：" name="isReply">
				<a-input v-model:value="formData.isReply" placeholder="请输入是否回复 0:未回复 1:已回复" allow-clear/>
			</a-form-item>
			<a-form-item label="是否显示 0:显示 1:隐藏：" name="isShow">
				<a-input v-model:value="formData.isShow" placeholder="请输入是否显示 0:显示 1:隐藏" allow-clear/>
			</a-form-item>
			<a-form-item label="回复用户id：" name="replyUser">
				<a-input v-model:value="formData.replyUser" placeholder="请输入回复用户id" allow-clear/>
			</a-form-item>
			<a-form-item label="回复内容：" name="replyContent">
				<a-input v-model:value="formData.replyContent" placeholder="请输入回复内容" allow-clear/>
			</a-form-item>
			<a-form-item label="回复时间：" name="replyTime">
				<a-date-picker v-model:value="formData.replyTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择回复时间" style="width: 100%"/>
			</a-form-item>
			<a-form-item label="组织id：" name="orgId">
				<a-input v-model:value="formData.orgId" placeholder="请输入组织id" allow-clear/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="appCommentForm">
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import appCommentApi from '@/api/biz/appCommentApi'
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			appCommentApi
				.appCommentSubmitForm(formDataParam, !formDataParam.commentId)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
